<%@ page language="java" %>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.commons.Constants"%>


<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>

<chiptrack:checkLogon sourcePage="/productSubstitutables.do" />
<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>
<HTML>
<HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet">

</HEAD>
<BODY bgColor="#ffffff" leftMargin="0" topMargin="0" marginheight="0" marginwidth="0" >

<SCRIPT language="JavaScript">

	function enableFields()
    {
		var form = document.productSubstitutablesForm;
		var substitutableProducts = form.selectedSubstitutableProducts;

		for(var i = 0 ; i < substitutableProducts.length ; i++ )
		{
			substitutableProducts.options[i].selected = true;
		}
	}

    function submitPage(action)
    {
        var form = document.productSubstitutablesForm;

        if(action == 'Products')
        {
            form.action.value = "Products";
            if( typeof form.selectedProduct != "undefined" )
            {
                form.selectedProduct.value = "";
            }
        }
        else if (action == 'Substitutes')
        {
            form.action.value = "Substitutes";
        }
        form.submit();
    }

    function saveProductSubstitutes()
	{
		var frm = document.productSubstitutablesForm;
		var targetPrd=frm.selectedSubstitutableProducts;
		enableFields();
        frm.action.value="save";
        frm.submit();
        return true;
    }

	var defaultStr = "";
	function sendOptionElementLeftToRight ( list1, list2, formName )
	{
		with ( formName.elements )
		{
			if ( list_copySelected ( list1,list2, defaultStr ) > 0 )
			{
				list_deleteSelected ( list1, defaultStr );
			}
		}
	}

	function sendOptionElementRightToLeft ( list1, list2, formName )
	{
		with ( formName.elements )
		{
			if ( list_copySelected ( list2, list1, defaultStr ) > 0 )
			{
				list_deleteSelected ( list2, defaultStr );
			}
		}
	}

	function list_copySelected(list1, list2, defaultStr)
	{

		if ( list1 == null || list2 == null ){
			return -1;
		}
		else if ( list1.length == 1 && list1.options[0].selected && list1.options[0].text == defaultStr){
			return -2;
		}

		var copyCount = 0;

		// unselect all in list2 first (IE5 bug)
		for (var i = 0; i < list2.length; i++ ){
			list2.options[i].selected = false;
		}

		// copy the selected items from list1 to list2
		for (var i = 0; i < list1.length; i++ ){
			if ( list1.options[i].selected ){
				list2.options[list2.length] = new Option(list1.options[i].text, list1.options[i].value);
				copyCount++;
			}
		}

		// remove the default str from list2 if neccesary
		if ( list2.length > 1 && list2.options[0].text == defaultStr ){
			list2.options[0] = null;
		}

		return copyCount;
	}

	function list_deleteSelected(list1, defaultStr)
	{
		// assertions
		if ( list1 == null ){
			return -1;
		}
		else if ( list1.length == 1 && list1.options[0].selected && list1.options[0].text == defaultStr){
			return -2;
		}

		// copy the selected items from list1 to the array
		// use trick with array because of IE5 bug
		var deleteCount = 0;
		var sel = new Array(list1.length);

		for (var i = 0; i < list1.length; i++ ){
			if ( list1.options[i].selected ){
				list1.options[i].selected = false;
				sel[deleteCount++] = i;
			}
		}

		for (var i = deleteCount - 1; i > -1 ; i-- ){
			list1.options[sel[i]] = null;
		}

		if ( list1.length == 0 )
			list1.options[0] = new Option(defaultStr, defaultStr);

		return deleteCount;
	}

</SCRIPT>
<!-- Top Header Start-->
<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->

<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
	<TD width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>

    <!-- Side Navigation start -->
    <%@ include file="/jsp/operations/product_menu.jsp" %></TD>
    <!-- end of the side navigations -->

    <TD bgcolor="#ffffff">
    <IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
    <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
    <TD valign="top" width="100%">

    <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
    <BR>

   	<html:form name="productSubstitutablesForm" action="/productSubstitutables" method="POST" enctype="multipart/form-data" type="com.scalar.chiptrack.operations.process.forms.ProductSubstitutablesForm" scope="request">
        <input type="hidden" name="action" value="">
        <TABLE width='85%' align=right cellpadding='0' cellspacing='0' border='0'>
            <TR><Td class="NArialW" align=center>Product Substitutables</td></tr>
            <TR><TD><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'></TD></TR>
            <tr><td align="left" class='NArialL'>&nbsp;<FONT id="errors" color='FF0000'></font></td></tr>
            <TR><TD><chiptrack:errors styleClass="NArialL"/></TD></TR>
            <TR><TD><chiptrack:info styleClass="NArialL"/></TD></TR>
             <logic:equal name="showMessage" value="TRUE">
                <TR><TD><FONT class='NArialS'color='blue'><B><%=Constants.NO_MODIFY_RIGHTS %></B></FONT></TD></TR>
            </logic:equal>

            <TR><TD><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'></TD></TR>

            <TR>
                <TD width="100%">
                    <TABLE border="0" width="100%">
                        <TR>
                           <TD class='NArialL' width='25%'> Product Type :<br></TD>
                            <TD class="NArialL" width='75%'>
                                <html:select property="productType" styleClass="txfield" onchange="javascript:submitPage('Products');">
                                    <html:option value="">-----Select-----</html:option>
                                    <html:option value="BOARD">BOARD</html:option>
                                    <html:option value="KIT">KIT</html:option>
                                    <html:option value="CHIP">CHIP</html:option>
                                    <html:option value="Die Bank">Die Bank</html:option>
                                </html:select>
                            </TD>
                        </TR>
                    </TABLE>
                </TD>
			</TR>
            <TR><TD>&nbsp;</TD></TR>
            <logic:notEqual name="productSubstitutablesForm" property="productType" value="">
            <TR>
                <TD width="100%">
                    <TABLE border="0" width="100%">
                        <TR>
                           <TD class='NArialL' width='25%'> Product :<br></TD>
                            <TD class="NArialL" width='75%'>
                                <html:select property="selectedProduct" styleClass="txfield" onchange="javascript:submitPage('Substitutes');">
                                    <html:option value="">-----Select-----</html:option>
                                    <html:options collection="productList" property="value" labelProperty="label"/>
                                </html:select>
                            </TD>
                        </TR>
                    </TABLE>
                </TD>
			</TR>
            </logic:notEqual>

            <TR><TD>&nbsp;</TD></TR>

            <logic:notEqual name="productSubstitutablesForm" property="productType" value="">
                <logic:notEqual name="productSubstitutablesForm" property="selectedProduct" value="">
                <TR>
                    <TD width="100%">
                        <TABLE border="0" width="100%">
                                <TR>
                                    <TD class="NArialL" width="22%"> Select Substitutes :</TD>
                                    <TD width="5%">&nbsp;</TD>
                                    <TD class="NArialL" width="73%"> Selected Substitutes :</TD>
                                </TR>
                                <tr>
                                    <td width="22%">
                                        <html:select property="substitutableProduct" styleClass="emptyList" size="12" multiple = "true">
                                            <html:options collection="substitutableProductList" property="value" labelProperty="label"/>
                                        </html:select>
                                    </td>
                                    <td width="5%">
                                        <input type="button" value=">>" onclick="sendOptionElementLeftToRight(substitutableProduct, selectedSubstitutableProducts, document.productSubstitutablesForm);">
                                        <br>
                                        <input type="button" value="<<"  onclick="sendOptionElementRightToLeft(substitutableProduct, selectedSubstitutableProducts, document.productSubstitutablesForm);">
                                    </td>
                                    <td width="73%">
                                        <html:select property="selectedSubstitutableProducts" styleClass="emptyList" size="12" multiple = "true" >
                                            <html:options collection="selectedSubstitutableProductList" property="value" labelProperty="label"/>
                                        </html:select>
                                    </td>
                                </tr>
                        </TABLE>
                    </TD>
                </TR>
                <TR><TD>&nbsp;</TD></TR>
                <tr>
                    <TD width="100%">
                        <TABLE width="75%">
                            <td width="100%" align=center>
                            <chiptrack:restrictedLink functionality='<%=Functionality.PRODUCT_SUBSTITUTABLES%>' action='<%=User.USER_ACTION_MODIFY%>' href='javascript:saveProductSubstitutes();' onmouseover="window.status='Click to Save Product Substitutes';return true" onmouseout="window.status='';return true">
                                <img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" ALIGN="BOTTOM" BORDER="0"  width="53" height="17" title="Click here to Save Product Substitutes"/>
                            </chiptrack:restrictedLink>
                            &nbsp;<a href="<%=chipTracContext%>/productSubstitutables.do" onmouseover="window.status='Click to Cancel';return true" onmouseout="window.status='';return true"  title="Click here to Cancel"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" width="53" height="17"></a>
                            </td>
                        </TABLE>
                    </TD>
                </tr>
                </logic:notEqual>
            </logic:notEqual>
            <tr height="30"><td colspan="4">&nbsp;</td></tr>
        </TABLE>
    </html:form>

<!-- till here -->
<BR><BR><BR></TD>
<TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
</TR>
</TABLE>

<!-- Footer starts here -->
<%@ include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->

</BODY>
</HTML>